package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import java.io.IOException;
import java.time.Duration;

public class UserPage extends Utils {
    public static String url = "http://127.0.0.1:9580/?";
    public static String res = "";

    public UserPage() {
        super(url);
    }

    public void enterUserPage() throws IOException {
        //点击进入个人发帖页面
        driver.findElement(By.cssSelector("#index_nav_avatar")).click();
        driver.findElement(By.cssSelector("#index_user_profile")).click();
        getScreenShot(getClass().getName());

        //从个人页面进入我的帖子
        driver.findElement(By.cssSelector("#profile_article_body > li:nth-child(1) > div.card.timeline-event-card > div > div > div > div > div.text-truncate > a")).click();
        driver.findElement(By.cssSelector("#details_btn_like_count"));
        getScreenShot(getClass().getName());

        //从我的帖子退出至首页
        driver.findElement(By.cssSelector("#nav_board_index > a > span.nav-link-title")).click();
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong"));

        //点击进入个人信息页面
        driver.findElement(By.cssSelector("#index_nav_avatar")).click();
        driver.findElement(By.cssSelector("#index_user_settings")).click();
        getScreenShot(getClass().getName());

        //从个人信息页面退出至首页
        driver.findElement(By.cssSelector("#nav_board_index > a > span.nav-link-title")).click();
        driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong"));
    }

    //基本测试
    public void modifyInformation() throws IOException, InterruptedException {
        //进入个人信息页面
        driver.findElement(By.cssSelector("#index_nav_avatar")).click();
        driver.findElement(By.cssSelector("#index_user_settings")).click();

        //修改个人资料(不做成功测试
        //与当前信息相同情况下
        //昵称相同
        driver.findElement(By.cssSelector("#setting_input_nickname")).clear();
        driver.findElement(By.cssSelector("#setting_input_nickname")).sendKeys("Morningstar");
        driver.findElement(By.cssSelector("#setting_submit_nickname")).click();
        res = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
        res = res.substring(res.lastIndexOf("\n") + 1).trim(); // 找到最后一个换行符，截取后面的内容
        getScreenShot(getClass().getName());
        Thread.sleep(300);
        getScreenShot(getClass().getName());
        assert res.equals("参数校验失败");
//        //邮箱相同
//        driver.findElement(By.cssSelector("#setting_input_email")).sendKeys("1");
//        res = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
//        res = res.substring(res.lastIndexOf("\n") + 1).trim(); // 找到最后一个换行符，截取后面的内容
//        assert res.equals("参数校验失败");
//        //电话号码相同
//        driver.findElement(By.cssSelector("#setting_input_phoneNum")).sendKeys("2");
//        res = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
//        res = res.substring(res.lastIndexOf("\n") + 1).trim(); // 找到最后一个换行符，截取后面的内容
//        assert res.equals("参数校验失败");
//        //个人简介相同
//        driver.findElement(By.cssSelector("#settings_textarea_remark")).sendKeys("3");
//        res = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div")).getText();
//        res = res.substring(res.lastIndexOf("\n") + 1).trim(); // 找到最后一个换行符，截取后面的内容
//        assert res.equals("参数校验失败");
    }

    public void modifyPassword() throws IOException, InterruptedException {
        //进入个人信息页面
        driver.findElement(By.cssSelector("#index_nav_avatar")).click();
        driver.findElement(By.cssSelector("#index_user_settings")).click();

        //下滑页面
        WebElement ele = driver.findElement(By.cssSelector("#settings_submit_password"));
        Actions actions = new Actions(driver);
        //perform作用：为了在页面看到效果
        actions.moveToElement(ele).perform();

        //不填写密码
//        driver.findElement(By.cssSelector("#settings_input_oldPassword")).clear();
//        driver.findElement(By.cssSelector("#settings_submit_password")).click();
//        driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div.col.d-flex.flex-column > div > div:nth-child(14) > div > div > form > div:nth-child(1) > div > div"));
//        assert res.equals("原密码不能为空");
//        driver.findElement(By.cssSelector("#settings_input_newPassword")).clear();
//        driver.findElement(By.cssSelector("#settings_submit_password")).click();
//        res = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div.col.d-flex.flex-column > div > div:nth-child(14) > div > div > form > div:nth-child(2) > div > div")).getText();
//        assert res.equals("新密码不能为空");
//        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).clear();
//        driver.findElement(By.cssSelector("#settings_submit_password")).click();
//        res = driver.findElement(By.cssSelector("#bit-forum-content > div.page-body > div > div > div > div.col.d-flex.flex-column > div > div:nth-child(14) > div > div > form > div:nth-child(3) > div > div")).getText();
//        assert res.equals("确认密码不能为空");

        //原密码错误
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("1");
        driver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("12");
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("12");
        driver.findElement(By.cssSelector("#settings_submit_password")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        res = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        getScreenShot(getClass().getName());
        Thread.sleep(300);
        getScreenShot(getClass().getName());
        assert res.equals("警告");

        //原密码与新密码相同
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).clear();
        driver.findElement(By.cssSelector("#settings_input_newPassword")).clear();
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).clear();
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("112233");
        driver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("112233");
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("112233");
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        res = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        getScreenShot(getClass().getName());
        Thread.sleep(300);
        getScreenShot(getClass().getName());
        assert res.equals("警告");

        //新密码与确认密码不同
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).clear();
        driver.findElement(By.cssSelector("#settings_input_newPassword")).clear();
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).clear();
        driver.findElement(By.cssSelector("#settings_input_oldPassword")).sendKeys("112233");
        driver.findElement(By.cssSelector("#settings_input_newPassword")).sendKeys("112");
        driver.findElement(By.cssSelector("#settings_input_passwordRepeat")).sendKeys("233");
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
        res = driver.findElement(By.cssSelector("body > div.jq-toast-wrap.bottom-right > div > h2")).getText();
        getScreenShot(getClass().getName());
        Thread.sleep(300);
        getScreenShot(getClass().getName());
        assert res.equals("警告");

    }
}
